﻿
@{
    ViewBag.Title = "颜色匹配";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}

@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "工单管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var colors = ViewBag.Colors as List<IOA.MES.DataAccess.VModel.VColorSet>;
}
@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.CellColor}}：</label>
                        <div class="layui-input-inline">
                            <select id="selCellColor" lay-filter="selCellColor">
                                @foreach (var item in colors.Where(_ => _.ColorType == ColorType.CellColor).ToList())
                                {
                                    <option value="@item.ColorCode">@item.Color</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.ComponentColor}}：</label>
                        <div class="layui-input-inline">
                            <select id="selComponentColor" lay-filter="selComponentColor">
                                @foreach (var item in colors.Where(_ => _.ColorType == ColorType.ComponentColor).ToList())
                                {
                                    <option value="@item.ColorCode">@item.Color</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnAdd" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-add-1 layuiadmin-button-btn"></i>
                        </button>
                        <button id="btnSearch" type="button"></button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var orderNo = "@ViewBag.OrderNo",
            workOrderNo = "@ViewBag.WorkOrderNo";
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form','laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });
                var options = {
                    elem: '#table'
                    , url: '/ParaSetting/QueryWorkOrderColors'
                    , method: "post"
                    , where: { orderNo: orderNo, workOrderNo: workOrderNo }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers" }
                        , { field: 'CellColorCode', title: '电池片颜色代码', minWidth: 200 }
                        , { field: 'CellColor', title: '电池片颜色', minWidth: 200 }
                        , { field: 'ComponentColorCode', title: '组件颜色代码', minWidth: 150 }
                        , { field: 'ComponentColor', title: '组件颜色', minWidth: 150 }
                        , {
                            title: '操作', width: 100, fixed: "right", templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteMatch(' + d.PkId + ')"><i class="layui-icon">&#xe640;</i>删除</button>';
                                return str;
                            }
                        }
                    ]]
                };
                $("#btnSearch").click(function () {
                    queryTable();
                });
                $("#btnAdd").click(function () {
                    var loadId = layer.load();
                    $.ajax({
                        url: "/ParaSetting/AddColorMatch",
                        data: {
                            model: {
                                OrderNo: orderNo,
                                WorkOrderNo: workOrderNo,
                                CellColorCode: $("#selCellColor").val().trim(),
                                ComponentColorCode: $("#selComponentColor").val().trim()
                            }
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            queryTable();
                        }
                    });
                    return false;
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function deleteMatch (matchId){
            var loadId = layer.load();
            $.ajax({
                url: "/ParaSetting/RemoveColorMatch",
                data: {
                    model: {
                        PkId: matchId,
                        OrderNo: orderNo,
                        WorkOrderNo: workOrderNo
                    }
                },
                complete: function () {
                    layer.close(loadId);
                },
                success: function (res) {
                    if (res.ErrCode != 200) {
                        layer.msg(res.ErrMsg, { icon: 5 });
                        return;
                    }
                    $("#btnSearch").click();
                }
            });
        }
    </script>
}
